package com.webdiy.hr.impl;

import com.sys.plat.bean.PageBean;
import com.sys.plat.service.base.BaseServiceImpl;
import com.webdiy.dao.model.HrInfo;
import com.webdiy.dao.model.HrInfoViewNotMail;
import com.webdiy.hr.iface.ZhaoPinSerice;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import java.sql.SQLException;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: mabo
 * Date: 2010-1-6
 * Time: 20:07:37
 * To change this template use File | Settings | File Templates.
 */
public class ZhaoPinSericeImpl extends BaseServiceImpl implements ZhaoPinSerice {


    public void findList(PageBean pageBean,String tags) {
        if (tags != null && tags.equals("notMail")) {
            pageBean.setHql("FROM HrInfoViewNotMail _t");
        }else{
            pageBean.setHql("FROM HrInfoView _t");
        }
        this.page(pageBean);
    }



    public List findCompanyList(String hId,String tags) {
        HrInfo h = (HrInfo) dao.getObject(HrInfo.class, hId);
        if(tags != null && tags.equals("notMail")){
            return dao.listByHql("FROM HrInfo WHERE companyName = ? AND send = '3' AND mail is null AND hrConfig = ? ORDER BY createDate DESC", h.getCompanyName(),h.getHrConfig().getId());
        }else{
            return dao.listByHql("FROM HrInfo WHERE companyName = ? AND send = '3' AND mail is not null  AND hrConfig = ? ORDER BY createDate DESC", h.getCompanyName(),h.getHrConfig().getId());
        }
        //To change body of implemented methods use File | Settings | File Templates.
    }


    public void updateJobToDFS(HrInfoViewNotMail h) {
        String[] ids = h.getId().split(";");
        for (String id : ids) {
            HrInfo _h = (HrInfo) dao.getObject(HrInfo.class, id);
            _h.setSend(h.getSend());
            dao.updateObject(_h);
        }
    }

    public void sendListByExt(PageBean pageBean,String tags) {
        if(tags != null && tags.equals("notMail")){
            pageBean.setHql("FROM HrInfo _t left join fetch _t.hrConfig WHERE  _t.send = '4' AND _t.finish = '1' ORDER BY _t.id");
        }else{
            pageBean.setHql("FROM HrInfo _t left join fetch _t.hrConfig WHERE  _t.send = '1' AND _t.finish = '1' ORDER BY _t.id");
        }
        //pageBean.setHql("FROM HrInfo hi");
        this.page(pageBean);
        /*List list = dao.listByHql("FROM HrInfo hi left join fetch hi.hrConfig WHERE  hi.send = '1' AND finish = '1'");
        System.out.println(list);*/
    }

    public String[] getSendMailCount(String configId) {
        //select send,count(*) from (select * from hr_info where (send = '1' or send = '4') and hr_config_id = '402881d726bcd2fc0126bce10d790001' and finish = '1') t group by send
        //SELECT send,count(send) FROM HrInfo WHERE hrConfig = ? AND finish = '1' GROUP BY send
        //List<Object[]> list = dao.listByHql("SELECT _t2.send,_t2.count(_t2.send) FROM " +
        //        "(SELECT * FROM HrInfo _t WHERE (_t.send = '1' OR _t.send = '4') AND _t.hrConfig = ? AND _t.finish = '1') _t2 GROUP BY _t2.send", configId);
        String[] strs = new String[]{"0","0"};
        final String sql = "select send,count(*) from (select * from hr_info where (send = '1' or send = '4') and hr_config_id = '"+configId+"' and finish = '1') t group by send";
        List<Object[]> list = dao.getHibernateTemplate().executeFind(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                return session.createSQLQuery(sql).list();
            }
        });
        
        for (Object[] objects : list) {
            if(objects[0].toString().equals("1")) {
                strs[0] = objects[1].toString();
            }

            if(objects[0].toString().equals("4")) {
                strs[1] = objects[1].toString();
            }
        }
        return strs;  //To change body of implemented methods use File | Settings | File Templates.
    }


    public static void main(String[] args) {
        ZhaoPinSericeImpl zp = new ZhaoPinSericeImpl();
    }

}
